package com.yifi.common.utils;

import java.util.List;

import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.SysUtil;

public class IotUtils {

	/**
	 * * 查询该产权下的所有设备
	 * 
	 */
	public static List<Record> getYxsbALL(String c_pro) {
		List<Record> list = getYxsbALL(c_pro, "ccode", "cname");
		return list;
	}

	public static List<Record> getYxsbALL(String c_pro, String cvalue, String text) {

		String sql = "select id  as " + cvalue + ",cname as " + text + " from mes_eam_yxsb where c_pro='" + c_pro
				+ "' order by isort";
		List<Record> list = SysUtil.getListDDL(sql, text, cvalue, "--全部--", "%");

		return list;
	}

	public static List<Record> getYxsbALL_Dn(String c_pro, String cvalue, String text) {

		String sql = "select id  as " + cvalue + ",cname as " + text + " from mes_eam_yxsb where c_pro='" + c_pro
				+ "' and dn_flag='1' order by isort";
		List<Record> list = SysUtil.getListDDL(sql, text, cvalue, "--全部--", "%");

		return list;
	}

	public static List<Record> getYxsbALL(String c_pro, String cvalue, String text, String condition) {

		String sql = "select t1.id  as " + cvalue + ",t1.cname as " + text + " from mes_eam_yxsb t1 where t1.c_pro='"
				+ c_pro + "'" + condition + " order by t1.isort";
		List<Record> list = SysUtil.getListDDL(sql, text, cvalue, "--全部--", "%");

		return list;
	}

	public static List<Record> getYxsbOn(String c_pro) {
		List<Record> list = getYxsbOn(c_pro, "ccode", "cname");
		return list;
	}

	/**
	 * * 查询该产权下的所有有效的设备
	 * 
	 */
	public static List<Record> getYxsbOn(String c_pro, String cvalue, String text) {
		String sql = "select id  as " + cvalue + ",cname as " + text + " from mes_eam_yxsb where c_pro='" + c_pro
				+ "' and r_flag='有效'  order by isort";
		List<Record> list = SysUtil.getListDDL(sql, text, cvalue, "--全部--", "%");

		return list;
	}

	/**
	 * * 返回查询设备的SQL
	 * 
	 * @param c_pro 产权
	 * @param ctype 类型 0-全部，1-电能参数设备，2-环境参数设备
	 * 
	 */
	public static String getDevice(String c_pro, String ctype) {
		String sql = "select id from mes_eam_yxsb  ";

		if (ctype.equals("1")) {
			sql = "select deviceid from mes_eam_yxsb_sscs where paramid in (select ccode from mes_gy_gycs where cmark<>'环境' and  c_pro ='"
					+ c_pro + "' ) and  c_pro ='" + c_pro + "' group by deviceid ";
		} else if (ctype.equals("2")) {
			sql = "select deviceid from mes_eam_yxsb_sscs where paramid in (select ccode from mes_gy_gycs where cmark='环境' and  c_pro ='"
					+ c_pro + "' ) and  c_pro ='" + c_pro + "' group by deviceid ";
		}

		return sql;
	}

	public static List<Record> getLx(String c_pro) {
		String sql = "select id as ccode,cname from base_eam_type where c_pro='" + c_pro + "' order by id";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode", "--全部--", "%");
		return list;
	}

	public static List<Record> getLx(String c_pro, String condition) {
		String sql = "select id as ccode,cname from base_eam_type where c_pro='" + c_pro + "' " + condition
				+ " order by id";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode", "--全部--", "%");
		return list;
	}

	public static List<Record> getQy(String c_pro) {
		String sql = "select id as ccode,cname from base_eam_area where c_pro='" + c_pro + "' order by isort";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode", "--全部--", "%");
		return list;
	}

	public static List<Record> getXt(String c_pro) {
		String sql = "select id as ccode,cname from base_eam_system where c_pro='" + c_pro + "' order by isort";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode", "--全部--", "%");
		return list;
	}

	/**
	 * * 获取所有的累积量参数
	 * 
	 */
	public static List<Record> getParm_Sum(String c_pro) {
		String sql = "select ccode,cname  from mes_gy_gycs where c_pro='" + c_pro + "' and ctype='累积量' order by isort";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode");
		return list;
	}

	/**
	 * * 获取所有的报警参数
	 * 
	 */
	public static List<Record> getParm_Alarm(String c_pro) {
		String sql = "select ccode,cname  from mes_gy_gycs where   c_pro='" + c_pro
				+ "' and ccode in (select paramid from mes_eam_yxsb_sscs  where c_pro='" + c_pro
				+ "' and isalarm='是')  order by isort";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode", "--全部--", "%");
		return list;
	}

	/**
	 * * 获取所有的计算最大，最小，平均值的参数
	 * 
	 */
	public static List<Record> getParm_Cal(String c_pro) {
		String sql = "select ccode,cname  from mes_gy_gycs where c_pro='" + c_pro + "' and ctype<>'开关量' order by isort";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode");
		return list;
	}

	public static List<Record> getParm_Cal(String c_pro, String condition) {
		String sql = "select ccode,cname  from mes_gy_gycs where c_pro='" + c_pro + "' and ctype<>'开关量' " + condition
				+ "  order by isort";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode");
		return list;
	}

	/**
	 * * 获取所有的状态值
	 * 
	 */
	public static List<Record> getParm_Zt(String c_pro) {
		String sql = "select czt as ccode,czt as cname  from (select czt from tb_device_sbzt_data where   c_pro='"
				+ c_pro + "'  group by czt union select czt   from tb_device_sbzt_hist_data where   c_pro='" + c_pro
				+ "'  group by czt) tmp";
		List<Record> list = SysUtil.getListDDL(sql, "cname", "ccode", "--全部--", "%");
		return list;
	}

}
